package com.tian.print;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.postek.coyote.postekwifi.MainActivity;
import com.tian.print.depot.GoodsDetailActivity;
import com.tian.print.depot.OutDepotActivity;
import com.tian.print.http.BaseObserver;
import com.tian.print.http.RxSchedulers;
import com.tian.print.http.request.ApiRequest;

import butterknife.ButterKnife;
import butterknife.OnClick;


public class HomeActivity extends AppCompatActivity {

    private final static int PERMS_REQUEST_CODE = 203;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        ButterKnife.bind(this);
        checkPermission();
    }

    private void checkPermission() {
        int permissionCheck = ContextCompat.checkSelfPermission(this,
                Manifest.permission.WRITE_CALENDAR);
        if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
            //6.0版本或以上需请求权限
            String[] permissions = new String[]{Manifest.permission.
                    WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA, Manifest.permission.ACCESS_COARSE_LOCATION};
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1)
                requestPermissions(permissions, PERMS_REQUEST_CODE);
        }
    }

    @OnClick(R.id.btn_scan)
    public void onTapScan() {
        startActivity(new Intent(this, GoodsDetailActivity.class));
    }

    @OnClick(R.id.btn_input)
    public void onTapInput() {

        startActivity(new Intent(this, MainActivity.class));
    }

    @OnClick(R.id.btn_out)
    public void onTapOut() {
//        scanQRCode();
        Intent intent = new Intent(this, OutDepotActivity.class);
//        intent.putExtra("plateNoOld", result);
        startActivity(intent);
    }


    /**
     * 打开二维码扫描
     * activity需要去重写onactivityforresult方法，具体请参考MainActivity
     */
    public void scanQRCode() {
        IntentIntegrator integrator = new IntentIntegrator(this);
        // 设置要扫描的条码类型，ONE_D_CODE_TYPES：一维码，QR_CODE_TYPES-二维码
        integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
        integrator.setCaptureActivity(ZxScanActivity.class);
        //底部的提示文字，设为""可以置空
        integrator.setPrompt("请扫描二维码");
        //前置或者后置摄像头
        integrator.setCameraId(0);
        //扫描成功的「哔哔」声，默认开启
        integrator.setBeepEnabled(true);
//                    integrator.setBarcodeImageEnabled(true);//是否保留扫码成功时候的截图
        integrator.initiateScan();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.i("Ok", "-requestCode-" + requestCode + ",resultCode-" + resultCode);
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (result != null) {
            if (result.getContents() == null)
                Toast.makeText(this, "扫码取消！", Toast.LENGTH_LONG).show();
            else
                onZxCodeReturn(result.getContents());
        }
        //
        else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    public void onZxCodeReturn(String result) {
        Intent intent = new Intent(this, OutDepotActivity.class);
        intent.putExtra("plateNoOld", result);
        startActivity(intent);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == PERMS_REQUEST_CODE) {
            boolean granted = true;
            for (int grant : grantResults) {
                if (grant != PackageManager.PERMISSION_GRANTED) {
                    granted = false;
                    break;
                }
            }
            if (granted) {
            } else {
                Toast.makeText(this, "权限被拒绝", Toast.LENGTH_SHORT).show();
            }
            return;
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}
